package cn.tedu.fresheveryday.mapper;

import cn.tedu.fresheveryday.pojo.entity.Product;
import cn.tedu.fresheveryday.pojo.entity.ShopCart;
import cn.tedu.fresheveryday.pojo.vo.ShopCartSimpleVO;
import cn.tedu.fresheveryday.pojo.vo.ShopCartVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface ShopCartMapper {
    /**
     * 添加购物车
     * @param shopCart
     */
    @Insert(
            "insert into shop_cart (" +
                "id, product_id, money, quantity, user_id, created" +
            ") values (" +
                "#{id}, #{productId}, #{money}, #{quantity}, #{userId}, #{created}" +
            ")"
    )
    void insert(ShopCart shopCart);

    /**
     * 批量添加购物车
     * @param list
     */
    void insertList(List<ShopCart> list);

    /**
     * 根据id删除购物车
     * @param id
     */
    @Delete("delete from shop_cart where id = #{id}")
    void deleteById(String id);

    /**
     * 根据用户id删除购物车
     * @param userId
     */
    @Delete("delete from shop_cart where user_id = #{userId}")
    void deleteByUserId(String userId);

    /**
     * 根据id查询购物车
     * @param id
     * @return
     */
    @Select("select id, product_id, money, quantity, user_id from shop_cart where id = #{id}")
    @Result(column = "user_id", property = "userId")
    @Result(column = "product_id", property = "productId")
    @Result(
            column = "product_id", property = "product", javaType = Product.class,
            one = @One(select = "cn.tedu.fresheveryday.mapper.ProductMapper.selectById")
    )
    ShopCartVO selectById(String id);

    /**
     * 根据用户id查询购物车
     * @param userId 用户id
     * @return
     */
    @Select(
            "select id, product_id, money, quantity, user_id from shop_cart " +
            "where user_id = #{userId}"
    )
    @Result(column = "user_id", property = "userId")
    @Result(column = "product_id", property = "productId")
    @Result(
            column = "product_id", property = "product", javaType = Product.class,
            one = @One(select = "cn.tedu.fresheveryday.mapper.ProductMapper.selectById")
    )
    List<ShopCartVO> selectByUserId(String userId);

    /**
     * 根据用户id查询购物车数量
     * @param userId 用户id
     * @return
     */
    @Select("select count(*) from shop_cart where user_id = #{userId}")
    int selectCount(String userId);

    /**
     * 根据用户ID和商品id查询购物车
     * @param userId 用户id
     * @param productId 商品id
     */
    @Select("select id, quantity from shop_cart where user_id = #{userId} and product_id = #{productId}")
    ShopCartSimpleVO selectByUserIdAndProductId(
            @Param("userId") String userId,
            @Param("productId") int productId
    );

    /**
     * 根据id修改购物车中的商品数量
     * @param id 购物车id
     * @param quantity 购物车商品数量
     */
    @Update("update shop_cart set quantity = #{quantity} where id = #{id}")
    void updateQuantityById(@Param("id") String id, @Param("quantity") int quantity);

    /**
     * 根据id修改购物车金额
     * @param id 购物车id
     * @param money 购物车金额
     */
    @Update("update shop_cart set money = #{money} where id = #{id}")
    void updateMoneyById(String id, double money);
}
